package tests;
import static org.junit.Assert.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;

import model.Email;
import model.EmailBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
 * This class tests the Email class
 * 
 * @author buthainahaldosary
 * @since May 2013
 *
 * @version $Revision: 1.0 $
 */
public class TestEmail {

	Date date;
	int uid=-1;
	Timestamp currentTime;
	EmailBuilder emb=new EmailBuilder();
	Email em;
	
	/**
	 * This method is called before every test case to setup a an email instance with specified parameters.
	 */
	@Before
	public void setUp()
	{
	
	 date= new Date();
	 currentTime=(new Timestamp(date.getTime()));
	 em=emb.setTo("Sara@gmail.com").setFrom("Buthainah.aldosary@gmail.com").setSubject("Hello").setBody("Just dropping by to say hello")
			 .setRead(false).setTimestamp(currentTime).setFolderId(1).build();

	}
	/**
	 * This method is called after every test case to tear down the Email and EmailBuilder and force 
	 * garbage collection.
	 */
	@After
	public void tearDown() {
		emb = null;
		em = null;
		System.gc();

	}
	
	/**
	 * This test ensures that transfer object for email is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetEmailHashMap() {
		@SuppressWarnings("serial")
		HashMap<String, String> emailTransferObject = new HashMap<String, String>(){{
			put("Id", String.valueOf(-1));
			put("ToEmail", "Sara@gmail.com");
			put("FromEmail", "Buthainah.aldosary@gmail.com");
			put("Subject", "Hello");
			put("Body", "Just dropping by to say hello");
			put("Read", String.valueOf(false));
			put("Ts", String.valueOf(currentTime));
			put("FolderId", String.valueOf(1));
		}};
		
		assertEquals(emailTransferObject, em.getTransferObject());
		
	}
	
	/**
	 * This test ensures that transfer object for email is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetEmptyHashMap() {
		HashMap<String, String> emailTransferObject = new HashMap<String, String>();
		assertFalse(emailTransferObject.equals(em.getTransferObject()));
	}
		
	/**
	 * This test case tests the getId() method by ensuring that the id is equal to the one specified.
	 */
	@Test 
	public void testGetId() {		
		assertEquals(uid,em.getId());
	}
	/**
	 * This test case tests the setId() method by setting a new Id and ensuring that the email id equals the new id.
	 */
	@Test
	public void testSetId() {
		
		
		em.setId(4);
		assertEquals(4,em.getId());
	}
	/**
	 * This test case tests the getTo() method by ensuring that the to field equals to the one specified.
	 */
	@Test
	public void testGetTo() {
		assertEquals("Sara@gmail.com",em.getTo());
	}
	/**
	 * This test case tests the SetTo() method by setting a new to field and ensuring that the email to field equals the new to field.
	 */
	@Test
	public void testSetTo() {
		em.setTo("Laura@gmail.com");
		assertEquals("Laura@gmail.com",em.getTo());
	}

	/**
	 * This test case tests the GetFrom() method by ensuring that the from field equals to the one specified.
	 */
	@Test
	public void testGetFrom() {
		assertEquals("Buthainah.aldosary@gmail.com",em.getFrom());
	}
	/**
	 * This test case tests the SetFrom() method by setting a new from field and ensuring that the email from field equals the new from field.
	 */
	@Test
	public void testSetFrom() {
		em.setFrom("b_aldosa@concordia.ca");
		assertEquals("b_aldosa@concordia.ca",em.getFrom());
	}
	/**
	 * This test case tests the GetSubject() method by ensuring that the subject field equals to the one specified.
	 */
	@Test
	public void testGetSubject() {
		assertEquals("Hello",em.getSubject());
	}
	/**
	 * This test case tests the SetSubject() method by setting a new subject field and ensuring that the email subject field equals the new subject field.
	 */
	@Test
	public void testSetSubject() {
		em.setSubject("Meeting Confirmation");
		assertEquals("Meeting Confirmation",em.getSubject());
	}
	/**
	 * This test case tests the GetBody() method by ensuring that the body field equals to the one specified.
	 */
	@Test
	public void testGetBody() {
		assertEquals("Just dropping by to say hello",em.getBody());
	}
	/**
	 * This test case tests the SetBody() method by setting a new body field and ensuring that the email body field equals the new body field.
	 */
	@Test
	public void testSetBody() {
		em.setBody("Hello, I am declining the meeting tomorrow. Thank you.");
		assertEquals("Hello, I am declining the meeting tomorrow. Thank you.",em.getBody());
	}

	/**
	 * This test case tests the IsRead() method by ensuring that the read field equals to the one specified.
	 */
	@Test
	public void testIsRead() {
		assertEquals(false,em.isRead());
	}
	/**
	 * This test case tests the SetRead() method by setting a new read field and ensuring that the email read field equals the new read field.
	 */
	@Test
	public void testSetRead() {
		em.setRead(true);
		assertEquals(true,em.isRead());
	}
	/**
	 * This test case tests the GetTimestamp() method by ensuring that the timestamp field equals to the one specified.
	 */
	@Test
	public void testGetTimestamp() {
		Date date= new Date();
		Timestamp currentTime=(new Timestamp(date.getTime()));
		em.setTimestamp(currentTime);
		assertEquals(currentTime,em.getTimestamp());
	}
	/**
	 * This test case tests the SetTimestamp() method by setting a new timestamp field and ensuring that the email timestamp field equals the new timestamp field.
	 */
	@Test
	public void testSetTimestamp() {
		Date date= new Date();
		Timestamp currentTime=(new Timestamp(date.getTime()));
		em.setTimestamp(currentTime);
		assertEquals(currentTime,em.getTimestamp());
	}
	/**
	 * This test case tests the GetCurrentFolder() method by ensuring that the folder id equals to the one specified.
	 */
	@Test
	public void testGetFolderId() {
		
		assertEquals(1,em.getFolderId());
	}
	/**
	 * This test case tests the SetCurrentFolder() method by setting a new folder id field and ensuring that the email folder id field equals the new folder id field.
	 */
	@Test
	public void testSetFolderId() {
		
		em.setFolderId(3);
		assertEquals(3,em.getFolderId());
	}
	/**
	 * This test case tests the match() method by checking both cases if the email contains a text and not.
	 */
	@Test
	public void testMatch()
	{
		assertTrue(em.match("Hello"));
		assertFalse(em.match("Hi"));
	}


}
